using System;
using System.Linq;
using CMS.api.Entity;
using CMS.api.Data;
using Microsoft.EntityFrameworkCore;
using System.Threading.Tasks;

namespace CMS.api.Repository
{
    public class EfCURD<T> : ICURDable<T> where T : BaseEntity    // Where T:BaseEntity 表示这个 T类型必须是BaseEntity类型的子类 如:Users、Roles、UserRoles
    {
        /// <summary>
        /// 实例化上下文类,用于对数据库的增删改查
        /// </summary>
        /// <returns></returns>

        // 用于对数据库操作的变量
        private CMSDb _db;
        
        public EfCURD(CMSDb db)
        {
            _db = db;
        }

        /// <summary>
        /// 定义一个接受传入的实体变量,用于在内部使用验证
        /// </summary>
        private DbSet<T> _entity;


        /// <summary>
        /// 一个受保护的属性,这个属性用于实体变量是否为空，不为空那么就返回实体变量,如果为空那么就从数据库中查找传入的类去查找实体。
        /// 从而达到始终这个实体变量不能为空。
        /// </summary>
        /// <value></value>
        protected DbSet<T> Entity
        {
            get
            {
                if (_entity == null)
                {
                    _entity = _db.Set<T>();
                }
                return _entity;
            }
        }

        /// <summary>
        /// 实现获取一整张表
        /// </summary>
        /// <returns></returns>
        public IQueryable<T> Table
        {
            get
            {
                return Entity.AsQueryable<T>();
            }
        }
        /// <summary>
        /// 根据id删除用户
        /// </summary>
        /// <param name="id"></param>
        public void Delete(int id)
        {
            var user = Entity.Where(x => x.Id == id).FirstOrDefault();
            if (user == null)
            {
                throw new System.NullReferenceException();
            }
            Entity.Remove(user);
            _db.SaveChanges();
        }

        /// <summary>
        /// 根据Id查询某条记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public T GetById(int id)
        {
            var user = Entity.Where(x => x.Id == id).FirstOrDefault();
            if (user == null)
            {
                throw new System.NullReferenceException();
            }
            return user;
        }

        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="entity"></param>
        public void Inster(T entity)
        {
            entity.IsActived = true;
            entity.IsDeleted = false;
            entity.CreatedTime = DateTime.Now;
            entity.UpdatedTime = DateTime.Now;
            entity.DisplayOrder = 0;

            Entity.Add(entity);
            _db.SaveChanges();
        }

        /// <summary>
        /// 异步添加记录
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task InsterAsync(T entity)
        {
            entity.IsActived = true;
            entity.IsDeleted = false;
            entity.CreatedTime = DateTime.Now;
            entity.UpdatedTime = DateTime.Now;
            entity.DisplayOrder = 0;
            entity.Remarks = "";

            await Entity.AddAsync(entity);
            await _db.SaveChangesAsync();
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="entity"></param>
        public void Update(T entity)
        {
            entity.UpdatedTime = DateTime.Now;
            // Entity.Update(entity);
            _db.SaveChanges();
        }

        /// <summary>
        /// 异步更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task UpDateAsync(T entity)
        {
            entity.UpdatedTime = DateTime.Now;
            // Entity.Update(entity);
            await _db.SaveChangesAsync();
        }
    }

}